import {
	Column,
	CreateDateColumn,
	Entity,
	Index,
	PrimaryColumn,
	PrimaryGeneratedColumn,
	UpdateDateColumn,
} from "typeorm";

class DataDate {
	@CreateDateColumn({ name: "created_at" })
	createdAt: Date;

	@UpdateDateColumn({ name: "updated_at" })
	updatedAt: Date;
}

@Entity()
@Index("idx_email", ["email"], { unique: true })
@Index("idx_username", ["username"], { unique: true })
export class User {
	@PrimaryGeneratedColumn()
	id: number;

	@Column({ type: "varchar", length: 100, nullable: true })
	email: string;

	@Column({ type: "varchar", length: 255 })
	password: string;

	@Column({ type: "varchar", length: 20, comment: "用户名, 用于登录" })
	username: string;

	@Column({ type: "varchar", length: 100, comment: "昵称", nullable: true })
	nickname: string;

	@Column((type) => DataDate, { prefix: false })
	dataDate: DataDate;
}

@Entity()
export class Role {
	@PrimaryGeneratedColumn()
	id: number;

	@Column({ type: "varchar", length: 50, comment: "角色名" })
	name: string;

	@Column((type) => DataDate, { prefix: false })
	dataDate: DataDate;
}

@Entity()
export class Menu {
	@PrimaryGeneratedColumn()
	id: number;

	@Column({ type: "varchar", length: 50, comment: "菜单/API名称" })
	name: string;

	@Column({ type: "tinyint", comment: "类型[1:菜单, 2:API]", default: 1 })
	type: number;

	@Column({ type: "varchar", length: 100, comment: "路径", nullable: true })
	path: string;

	@Column({ type: "varchar", length: 50, comment: "图标", nullable: true })
	icon: string;

	@Column({ type: "int", comment: "排序", default: 0 })
	order: number;

	@Column({ type: "int", comment: "父级id", nullable: true, name: "parent_id" })
	parentId: number;

	@Column({
		type: "tinyint",
		comment: "是否显示, 1 - 显示; 0 - 隐藏",
		default: 1,
		name: "is_show",
	})
	isShow: number;

	@Column({
		type: "varchar",
		length: 100,
		comment: "前端组件路径",
		nullable: true,
	})
	component: string;

	@Column((type) => DataDate, { prefix: false })
	dataDate: DataDate;
}

@Entity()
export class UserRole {
	@PrimaryColumn({ type: "int", comment: "用户id", name: "user_id" })
	userId: number;

	@PrimaryColumn({ type: "int", comment: "角色id", name: "role_id" })
	roleId: number;

	@Column((type) => DataDate, { prefix: false })
	dataDate: DataDate;
}

@Entity()
export class RoleMenu {
	@PrimaryColumn({ type: "int", comment: "角色id", name: "role_id" })
	roleId: number;

	@PrimaryColumn({ type: "int", comment: "菜单id", name: "menu_id" })
	menuId: number;

	@Column((type) => DataDate, { prefix: false })
	dataDate: DataDate;
}

@Entity()
@Index("idx_token", ["token"])
export class UserToken {
	@PrimaryColumn({ type: "int", comment: "用户id", name: "user_id" })
	userId: number;

	@PrimaryColumn({ type: "varchar", length: 100 })
	token: string;

	@Column({ type: "tinyint", default: 1, comment: "状态[1:有效, 0:无效]" })
	status: number;

	@Column({
		type: "datetime",
		length: 0,
		comment: "到期时间",
		name: "expire_time",
	})
	expireTime: Date;

	@Column((type) => DataDate, { prefix: false })
	dataDate: DataDate;
}
